package com.enums;

/**
 * @Author：YuanHaHa
 * @Date：2024/7/6 2:33
 */
public enum OperationStatusEnum {
    /**
     *  枚举状态：SUCCESS，FAILURE，PENDING，CANCELED
     */
    SUCCESS(0, "操作成功"),
    FAILURE(1, "操作失败"),
    PENDING(2, "等待中"),
    CANCELED(3, "已取消");

    private int code;
    private String description;

    OperationStatusEnum(int code, String description){
        this.code = code;
        this.description = description;
    }

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 根据给定的操作状态代码返回对应的枚举值，增强了代码的安全性和易用性。
     * @param code
     * @return
     */
    public static OperationStatusEnum fromCode(int code){
        for (OperationStatusEnum status : values()) {
            if (status.getCode() == code){
                return status;
            }
        }
        throw new IllegalArgumentException("未知的操作状态码。");
    }
}
